package zhr.chapter10;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Example10_5 {
    public static void main(String[] args) {
        File helloFile=new File("d://","HelloWorld.txt");

        try {
            //创建文件
            boolean isCreated= helloFile.createNewFile();
            if(isCreated||helloFile.exists()){
                //以对象流的方式将mike写入文件heeloFile
                Student mike=new Student("Mike",23,21);
                FileOutputStream fileOut=new FileOutputStream(helloFile);
                ObjectOutputStream objectOut=new ObjectOutputStream(fileOut);
                objectOut.writeObject(mike);
                objectOut.close();
                fileOut.close();
                //读取
                FileInputStream fileIn=new FileInputStream(helloFile);
                ObjectInputStream objectIn=new ObjectInputStream(fileIn);
                Student stuMike=(Student) objectIn.readObject();
                System.out.println(stuMike.getName());
                System.out.println(stuMike.getNum());
                System.out.println(stuMike.getAge());
            }
        } catch (IOException | ClassNotFoundException e) {
            Logger.getLogger(Example10_5.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}
